#include "globalvars.h"
#include "smartdoor.h"

#include "ohos_init.h"
#include "cmsis_os2.h"

int g_door_status = 0;
int g_door_alarm_status = 0;

// int g_door_work_mode = 4;
// int g_door_control_flag = 0;


void Smartdoor_Task(void)
{
	Door_init(); //初始化门模块  
    printf("The door is activated successfully!!\r\n");
    usleep(100 * 1000);  //延时100ms
    while(1)
    {
        if(g_door_work_mode == 1)
        {
            if(g_door_control_flag == 1 && g_door_status == 0)
            {
                Door_switch(1);
                // usleep(3 * 1000 * 1000);
                usleep(400 * 1000);
                Door_switch(0);
                g_door_status = 1;
                g_door_control_flag = 0;
            }
            if(g_door_control_flag == 2 && g_door_status == 1)
            {
                Door_switch(2);
                // usleep(3 * 1000 * 1000);
                usleep(400 * 1000);
                Door_switch(0);
                g_door_status = 0;
                g_door_control_flag = 0;
            }
        }
        if(g_door_work_mode == 2)
        {
            if(g_door_flag == 1 && g_door_status == 0)
            {
                Door_switch(1);  //开门
                // usleep(3 * 1000 * 1000);
                usleep(400 * 1000);
                a:
                Door_switch(0); // 停机
                g_door_flag = 0; 
                g_door_status = 1;
            }
            if(g_door_flag == 0 && g_door_status == 1)
            {
                usleep(3 * 1000 * 1000);
                if(g_door_flag == 1)
                {
                    goto a;
                }
                Door_switch(2); // 关门
                // usleep(3 * 1000 *1000);
                usleep(400 * 1000);
                Door_switch(0); // 停机
                g_door_status = 0;
            }
        }
        if(g_door_work_mode == 3)
        {
            if(g_door_status == 0 && g_door_flag == 1)
            { 
                Door_switch(1);
                // usleep(3 * 1000 * 1000);
                usleep(400 * 1000);
                b:
                Door_switch(0);
                g_door_flag = 0;
                g_door_status = 1;
            }
            if(g_door_status == 1 && g_door_flag == 0 && g_door_control_flag == 0)
            {
                usleep(3 * 1000 * 1000);
                if(g_door_flag == 1)
                {
                    goto b;
                }
                Door_switch(2); // 关门
                // usleep(3 * 1000 *1000);
                usleep(400 * 1000);
                Door_switch(0); // 停机
                g_door_status = 0;   
            }

            if(g_door_control_flag == 1 && g_door_status == 0)
            {
                Door_switch(1);
                // usleep(3 * 1000 * 1000);
                usleep(400 * 1000);
                Door_switch(0);
                g_door_status = 1;
            }
            if(g_door_control_flag == 2 && g_door_status == 1 && g_door_flag == 0)
            {
                Door_switch(2);
                // usleep(3 * 1000 * 1000);
                usleep(400 * 1000);
                Door_switch(0);
                printf("111111111111111111111111111111111111111\n");
                g_door_control_flag = 0;
                g_door_status = 0;
            }
        }
        if(g_door_work_mode == 4)
        {
            if(g_door_status == 0 && g_door_control_flag == 1)
            {
                Door_switch(1);
                // usleep(3 * 1000 * 1000);
                usleep(400 * 1000);
                Door_switch(0);
                g_door_status = 1;
                g_door_control_flag = 0;
            }
            if(g_door_status == 1 && g_door_control_flag == 2)
            {
                Door_switch(2);
                // usleep(3 * 1000 * 1000);
                usleep(400 * 1000);
                Door_switch(0);
                g_door_status = 0;
                g_door_control_flag = 0;
            }
            if(g_door_status == 0 && g_door_flag == 1 && g_door_control_flag == 0)
            {   
                // Security_alarm(1);
                g_door_alarm_status = 1;
                usleep(3 * 1000 * 1000);
                // Security_alarm(0);
                g_door_alarm_status = 0;
                g_door_flag = 0;
            }
        }
        usleep(3 * 1000 * 1000);
    }
}

void Smartdoor_Entry(void)
{
	usleep(3 * 1000 * 1000); //延时3s
	osThreadAttr_t attr; //任务属性结构体
	attr.name = "Smartdoor_Task"; //任务名称
	attr.stack_size = 1024; //任务内存大小
	attr.priority = 25;
    if(osThreadNew((osThreadFunc_t)Smartdoor_Task, NULL, &attr) == NULL)
    {
        printf("[Smartdoor] Falied to create Smartdoor_Task!\n");
    }
    printf("[Smartdoor] Succeed to create Smartdoor_Task!\n");
}

SYS_RUN(Smartdoor_Entry);